package com.yanqu.road.server.channelserver.logic;

import com.google.protobuf.AbstractMessage;
import com.yanqu.road.pb.cross3.server.CrossChannelRouteProto;
import com.yanqu.road.server.connect.ChannelCross3LinkedControl;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.channelserver.manager.server.ChannelRegisterServerMgr;
import com.yanqu.road.server.protocol.Cross3Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.List;
import java.util.Map;

/**
 * 跨地区服务器发消息
 * （跨服发消息到跨地区用MessageHelper.sendChannelServer）
 */
public class ChannelMessageHelper {

    private static Logger logger = LogManager.getLogger(ChannelMessageHelper.class.getName());

    private static void sendPacket(long serverId, YanQuMessage packet) {
        ChannelCross3LinkedControl.send(serverId, packet.getPlayerId(), packet);
    }

    public static void sendToChannel(YanQuMessage message, int channelId) {
        for (long cross3Id : ChannelRegisterServerMgr.getCross3IdList(channelId)) {
            sendPacket(cross3Id, message);
        }
    }

    public static void sendOtherChannel(YanQuMessage message, int exceptChannelId) {
        for (int channelId : ChannelRegisterServerMgr.getChannelIdList()) {
            if (channelId != exceptChannelId) {
                for (long cross3Id : ChannelRegisterServerMgr.getCross3IdList(channelId)) {
                    sendPacket(cross3Id, message);
                }
            }
        }
    }

    public static void sendAllChannel(YanQuMessage message) {
        for (int channelId : ChannelRegisterServerMgr.getChannelIdList()) {
            for (long cross3Id : ChannelRegisterServerMgr.getCross3IdList(channelId)) {
                sendPacket(cross3Id, message);
            }
        }
    }

    public static void sendGamePacket(int gameCode, AbstractMessage.Builder<?> messageBuilder,
                                      int channelId, long gameServerId, long userId) {
        if (gameServerId == -1) {
            throw new RuntimeException("gameServerId error!");
        }

        CrossChannelRouteProto.RouteGameMsg.Builder builder = CrossChannelRouteProto.RouteGameMsg.newBuilder();
        builder.setGameCode(gameCode);
        builder.setGameServerId(gameServerId);
        builder.setUserId(userId);
        builder.setMsg(messageBuilder.build().toByteString());
        YanQuMessage message = YanQuMessageUtils.buildMessage(Cross3Protocol.C3_ROUTE_GAME_MSG, userId, builder);

        for (long cross3Id : ChannelRegisterServerMgr.getCross3IdList(channelId)) {
            sendPacket(cross3Id, message);
        }
    }

    public static void sendGamePacket(int gameCode, AbstractMessage.Builder<?> messageBuilder,
                                      int channelId, Map<Long, List<Long>> userIdsMap) {
        CrossChannelRouteProto.RouteBatchGameMsg.Builder builder = CrossChannelRouteProto.RouteBatchGameMsg.newBuilder();
        builder.setGameCode(gameCode);
        for (Map.Entry<Long, List<Long>> entry : userIdsMap.entrySet()) {
            CrossChannelRouteProto.BatchGameTemp.Builder temp = CrossChannelRouteProto.BatchGameTemp.newBuilder();
            temp.setGameServerId(entry.getKey());
            temp.addAllUserId(entry.getValue());
            builder.addGameServer(temp);
        }
        builder.setMsg(messageBuilder.build().toByteString());
        YanQuMessage message = YanQuMessageUtils.buildMessage(Cross3Protocol.C3_ROUTE_BATCH_GAME_MSG, 0, builder);

        for (long cross3Id : ChannelRegisterServerMgr.getCross3IdList(channelId)) {
            sendPacket(cross3Id, message);
        }
    }

    // 发送到某个渠道的全部游戏服
    public static void sendAllGamePacket(int gameCode, AbstractMessage.Builder<?> messageBuilder, int channelId) {
        CrossChannelRouteProto.RouteGameMsg.Builder builder = CrossChannelRouteProto.RouteGameMsg.newBuilder();
        builder.setGameCode(gameCode);
        builder.setGameServerId(-1);
        builder.setUserId(0);
        builder.setMsg(messageBuilder.build().toByteString());
        YanQuMessage message = YanQuMessageUtils.buildMessage(Cross3Protocol.C3_ROUTE_GAME_MSG, 0, builder);

        for (long cross3Id : ChannelRegisterServerMgr.getCross3IdList(channelId)) {
            sendPacket(cross3Id, message);
        }
    }

}
